package com.luych.toolbox.data.service.core;

import com.luych.toolbox.common.enums.DataSourceType;
import com.luych.toolbox.common.feign.param.data.service.DataSourceParam;
import com.luych.toolbox.data.service.constant.Constant;
import com.luych.toolbox.data.service.core.dm.*;
import com.luych.toolbox.data.service.core.mysql.*;
import com.luych.toolbox.data.service.entity.DataSource;
import com.luych.toolbox.data.service.entity.DataTypeSetting;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class CoreBuilder {

    public static CoreConnection buildConnection(DataSourceParam dataSource) {
        return buildConnection(dataSource.getType(), dataSource.getHost(), dataSource.getPort(), dataSource.getUser(), dataSource.getPass(), dataSource.getDb());
    }

    public static CoreConnection buildConnection(DataSource dataSource) {
        return buildConnection(dataSource.getType(), dataSource.getHost(), dataSource.getPort(), dataSource.getUser(), dataSource.getPass(), dataSource.getDb());
    }

    public static CoreConnection buildConnection(DataSourceType type, String host, int port, String user, String pass, String db) {
        switch (type) {
            case MYSQL: return new MysqlConnection(host, port, user, pass, db);
            case DM: return new DmConnection(host, port, user, pass, db);
            default: throw new UnsupportedOperationException();
        }
    }

    public static CoreDatabase buildDataBase(DataSourceType type, CoreDatabase database) {
        switch (type) {
            case MYSQL: return new MysqlDatabase(database);
            case DM: return new DmDatabase(database);
            default: throw new UnsupportedOperationException();
        }
    }

    public static CoreView buildView(DataSourceType type, CoreView view) {
        switch (type) {
            case MYSQL: return new MysqlView(view);
            case DM: return new DmView(view);
            default: throw new UnsupportedOperationException();
        }
    }

    public static CoreTable buildTable(DataSourceType type, CoreTable table) {
        switch (type) {
            case MYSQL: return new MysqlTable(table);
            case DM: return new DmTable(table);
            default: throw new UnsupportedOperationException();
        }
    }

    public static CoreColumn buildColumn(DataSourceType type, CoreColumn column) {
        switch (type) {
            case MYSQL: return new MysqlColumn(column);
            case DM: return new DmColumn(column);
            default: throw new UnsupportedOperationException();
        }
    }

    public static CoreDataType buildDataType(DataSourceType dataSourceTypeFrom,
                                             DataSourceType dataSourceTypeTo,
                                             CoreDataType dataType) {
        if (Objects.equals(dataSourceTypeFrom, dataSourceTypeTo)) {
            return dataType;
        }
        for (DataTypeSetting setting : Constant.DATA_TYPE_SETTINGS) {
            if (Objects.equals(setting.getDataSourceTypeFrom(), dataSourceTypeFrom)
                    && Objects.equals(setting.getDataSourceTypeTo(), dataSourceTypeTo)
                    && Objects.equals(setting.getDataTypeFrom(), dataType)) {
                return setting.getDataTypeTo();
            }
        }
        switch (dataSourceTypeTo) {
            case MYSQL: return MysqlDataType.valueOf(dataType.getName());
            case DM: return DmDataType.valueOf(dataType.getName());
            default: throw new UnsupportedOperationException();
        }
    }


    public static List<String> listDataTypes(DataSourceType type) {
        switch (type) {
            case MYSQL: return Arrays.stream(MysqlDataType.values()).map(Enum::name).collect(Collectors.toList());
            case DM: return Arrays.stream(DmDataType.values()).map(Enum::name).collect(Collectors.toList());
            default: throw new UnsupportedOperationException();
        }
    }
}
